import type { BasicColumn, FormSchema } from '@/components/Table'
import { useRender } from '@/components/Table'
import { deptApi } from '@/api/sys/deptApi'
import { roleApi } from '@/api/sys/roleApi'
import { useDictStoreWithout } from '@/store/modules/dict'

export const columns: BasicColumn[] = [
  {
    title: '账号',
    dataIndex: 'account',
    width: 120,
  },
  {
    title: '姓名',
    dataIndex: 'realName',
  },
  {
    title: '手机',
    dataIndex: 'employeeMobile',
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
  },
  {
    title: '状态',
    dataIndex: 'enabled',
    width: 60,
    customRender: ({ value }) => useRender.renderDictTag(value, 'STATE'),
  },
]

export const searchFormSchema: FormSchema[] = [
  {
    label: '账号',
    field: 'account',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    label: '姓名',
    field: 'realName',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    label: '手机号',
    field: 'employeeMobile',
    component: 'Input',
    colProps: { span: 8 },
  },
  {
    label: '状态',
    field: 'enabled',
    component: 'Select',
    componentProps: {
      options: useDictStoreWithout().getDictOptions('STATE'),
    },
    colProps: { span: 8 },
  },
  {
    label: '创建时间',
    field: 'createTime',
    component: 'RangePicker',
    colProps: { span: 8 },
  },
]

export const formSchema: FormSchema[] = [
  {
    field: 'employeeId',
    show: false,
    component: 'Input',
  },
  {
    label: '账号',
    field: 'account',
    required: true,
    component: 'Input',
    dynamicDisabled: ({ values }) => values.employeeId,
    ifShow: ({ values }) => !values.employeeId,
    rules: [
      {
        pattern: /^\w{4,16}$/gm,
        message: '只允许4 - 16位大小写字母下划线的组合.',
        trigger: 'blur',
      },
    ],
  },
  {
    label: '姓名',
    field: 'realName',
    required: true,
    component: 'Input',
  },
  {
    label: '手机号',
    field: 'employeeMobile',
    required: true,
    component: 'Input',
    dynamicDisabled: ({ values }) => values.employeeId,
    ifShow: ({ values }) => !values.employeeId,
    rules: [
      {
        pattern: /^1(?:3\d|4[5-79]|5[0-35-9]|6[5-7]|7[0-8]|8\d|9[189])\d{8}$/,
        message: '请输入正确的手机号码',
        trigger: 'blur',
      },
    ],
  },
  {
    label: '部门',
    field: 'deptIds',
    required: false,
    component: 'ApiTreeSelect',
    componentProps: {
      api: deptApi.tree,
      parentFiled: 'parentDeptId',
      valueField: 'deptId',
      labelField: 'deptName',
      resultField: 'rows',
      treeCheckable: true,
    },
  },
  {
    field: 'remark',
    label: '备注',
    component: 'InputTextArea',
    required: false,
    componentProps: {
      placeholder: '请输入备注',
    },
  },
]

export const employeeRoleFormSchema: FormSchema[] = [
  {
    field: 'employeeId',
    show: false,
    component: 'Input',
  },
  {
    label: '用户名',
    field: 'account',
    required: true,
    component: 'Input',
    dynamicDisabled: true,
  },
  {
    label: '姓名',
    field: 'realName',
    required: true,
    component: 'Input',
    dynamicDisabled: true,
  },
  {
    label: '角色',
    field: 'roleIds',
    component: 'ApiSelect',
    componentProps: {
      params: { page: 1, size: 100, enabled: true },
      api: roleApi.list,
      resultField: 'rows',
      labelField: 'roleName',
      valueField: 'roleId',
      mode: 'tags',
    },
  },
]
